// 模拟一个数据库环境
const listData = [
  {
    id: 1,
    title: "一、",
    content: "白日放歌须纵酒，青春作伴好还乡。",
    likeNums: 11,
    isLiked: false,
  },
  {
    id: 2,
    title: "二、",
    content: "且将新火试新茶，诗酒趁年华。",
    likeNums: 22,
    isLiked: false,
  },
  {
    id: 3,
    title: "三、",
    content: "我有一瓢酒，可以慰风尘。",
    likeNums: 33,
    isLiked: false,
  },
  {
    id: 4,
    title: "四、",
    content: "绿蚁新醅酒，红泥小火炉。",
    likeNums: 44,
    isLiked: false,
  },
  {
    id: 5,
    title: "五、",
    content: "酒困路长惟欲睡，日高人渴漫思茶，敲门试问野人家。",
    likeNums: 55,
    isLiked: false,
  },
  {
    id: 6,
    title: "六、",
    content: "一生大笑能几回，斗酒相逢须醉倒。",
    likeNums: 66,
    isLiked: false,
  },
  {
    id: 7,
    title: "七、",
    content: "长风万里送秋雁，对此可以酣高楼。",
    likeNums: 77,
    isLiked: false,
  },
  {
    id: 8,
    title: "八、",
    content: "东篱把酒黄昏后，有暗香盈袖。",
    likeNums: 88,
    isLiked: false,
  },
];

const detailList = [
  {
    id: 1,
    author: "杜甫",
    dynasty: "唐",
    title: "闻官军收河南河北",
    detailContent:
      "剑外忽传收蓟北，初闻涕泪满衣裳。\n却看妻子愁何在，漫卷诗书喜欲狂。\n白日放歌须纵酒，青春作伴好还乡。\n即从巴峡穿巫峡，便下襄阳向洛阳。",
  },
  {
    id: 2,
    author: "苏轼",
    dynasty: "宋",
    title: "望江南·超然台作",
    detailContent:
      "春未老，风细柳斜斜。\n试上超然台上望，半壕春水一城花。烟雨暗千家。\n寒食后，酒醒却咨嗟。\n休对故人思故国，且将新火试新茶，诗酒趁年华。",
  },
  {
    id: 3,
    author: "韦应物",
    dynasty: "唐",
    title: "简卢陟",
    detailContent:
      "可怜白雪曲，未遇知音人。\n恓惶戎旅下，蹉跎淮海滨。\n涧树含朝雨，山鸟哢馀春。\n我有一瓢酒，可以慰风尘。",
  },
  {
    id: 4,
    author: "白居易",
    dynasty: "唐",
    title: "问刘十九",
    detailContent: "绿蚁新醅酒，红泥小火炉。\n晚来天欲雪，能饮一杯无？",
  },
  {
    id: 5,
    author: "苏轼",
    dynasty: "宋",
    title: "浣溪沙·簌簌衣巾落枣花",
    detailContent:
      "簌簌衣巾落枣花，村南村北响缲车。\n牛衣古柳卖黄瓜。\n酒困路长惟欲睡，日高人渴漫思茶。\n敲门试问野人家。",
  },
  {
    id: 6,
    author: "岑参",
    dynasty: "唐",
    title: "凉州馆中与诸判官夜集",
    detailContent:
      "弯弯月出挂城头，城头月出照凉州。\n凉州七里十万家，胡人半解弹琵琶。\n琵琶一曲肠堪断，风萧萧兮夜漫漫。\n河西幕中多故人，故人别来三五春。\n花门楼前见秋草，岂能贫贱相看老。\n一生大笑能几回，斗酒相逢须醉倒。",
  },
  {
    id: 7,
    author: "李白",
    dynasty: "唐",
    title: "宣州谢朓楼饯别校书叔云",
    detailContent:
      "弃我去者，昨日之日不可留；\n乱我心者，今日之日多烦忧。\n长风万里送秋雁，对此可以酣高楼。\n蓬莱文章建安骨，中间小谢又清发。\n俱怀逸兴壮思飞，欲上青天览明月。\n抽刀断水水更流，举杯消愁愁更愁。\n人生在世不称意，明朝散发弄扁舟。",
  },
  {
    id: 8,
    author: "李清照",
    dynasty: "宋",
    title: "醉花阴·薄雾浓云愁永昼",
    detailContent:
      "薄雾浓云愁永昼，瑞脑销金兽。\n佳节又重阳，玉枕纱厨，半夜凉初透。\n东篱把酒黄昏后，有暗香盈袖。\n莫道不销魂，帘卷西风，人比黄花瘦。",
  },
];

async function getDetailById(id) {
  const pro = new Promise((resolve) => {
    setTimeout(() => {
      let detail = {};
      detail = getDetailByIdFromDiffData(detail, id, listData);
      detail = getDetailByIdFromDiffData(detail, id, detailList);
      resolve(detail);
    }, 1000);
  });
  return pro;
}

function getDetailByIdFromDiffData(detail, id, dataOrigin) {
  for (let i = 0; i < dataOrigin.length; i++) {
    const item = dataOrigin[i];
    if (item.id === id) {
      detail = { ...detail, ...item };
      return detail;
    }
  }
  return detail;
}

function likeOrCancelLikeRequest(id) {
  if (typeof id !== "number") {
    return;
  }
  const result = {
    status: 0,
    likeNums: null,
    isLiked: null,
  };
  for (let i = 0; i < listData.length; i++) {
    const item = listData[i];
    if (item.id === id) {
      if (item.isLiked) {
        // 点过赞了，点赞数少1
        item.likeNums--;
        item.isLiked = false;
      } else {
        // 没有点过赞，点赞数加1
        item.likeNums++;
        item.isLiked = true;
      }
      result.status = 1;
      result.likeNums = item.likeNums;
      result.isLiked = item.isLiked;
      break;
    }
  }
  return result;
}

async function deleteRequest(id) {
  const pro = new Promise((resolve) => {
    setTimeout(() => {
      if (typeof id !== "number") {
        resolve(flase);
        return false;
      }
      for (let i = 0; i < listData.length; i++) {
        const item = listData[i];
        if (item.id === id) {
          listData.splice(i, 1);
          resolve(true);
          return true;
        }
      }
      resolve(flase);
      return false;
    }, 1000);
  });
  return pro;
}

function getListData() {
  return listData;
}
export { getListData, likeOrCancelLikeRequest, deleteRequest, getDetailById };
